package com.hnkywl.sts.interceptor;


import com.hnkywl.sts.listener.OnlineInfo;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2010-6-7
 * Time: 10:52:59
 * To change this template use File | Settings | File Templates.
 */
public class AuthInterceptor extends AbstractInterceptor {
    public static final String USER_SESSION_KEY = "GYLWL_YG_SESSION_KEY";
    public static final String USER_NAME_SESSION_KEY = "GYLWL_USERNAME_SESSION_KEY";
    public static final String USER_ORGAN_SESSION_KEY = "GYLWL_USERORGAN_SESSION_KEY";
    public static final String USER_MAJORORGAN_ID_SESSION_KEY = "GYLWL_USERMAJORORGANID_SESSION_KEY";
    public static final String USER_MAJORORGAN_CODE_SESSION_KEY = "GYLWL_USERMAJORORGANCODE_SESSION_KEY";
    public static final String USER_MAJORORGAN_SESSION_KEY = "GYLWL_USERMAJORORGAN_SESSION_KEY";
    public static final String USER_ORGAN_ID_SESSION_KEY = "GYLWL_USERORGANID_SESSION_KEY";
    public static final String USER_ROLE_ID_SET_SESSION_KEY = "USER_ROLE_ID_LIST_SESSION_KEY";
    public static final String USER_CXFW_SESSION_KEY = "USER_CXFW_SESSION_KEY";

    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
        ActionContext actionContext = invocation.getInvocationContext();
        Map session = actionContext.getSession();
        HttpServletRequest request = (HttpServletRequest) actionContext.get(ServletActionContext.HTTP_REQUEST);
        HttpServletResponse response = (HttpServletResponse) actionContext.get(ServletActionContext.HTTP_RESPONSE);
        if (session != null && session.get(USER_SESSION_KEY) != null) {
                return invocation.invoke();
        } else if (request.getParameter("sessionUser") != null) {
                return invocation.invoke();
        }

//        if (request.getHeader("x-requested-with") != null
//                && request.getHeader("x-requested-with")
//                .equalsIgnoreCase("XMLHttpRequest")) {//如果是ajax请求响应头会有，x-requested-with；
//            response.addHeader("sessionstatus", "timeout");
//            return null;
//        }
        return "login";
    }
}